package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.mapper.StaffMessageMapper;
import com.itheima.pojo.Paging;
import com.itheima.pojo.StaffMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class StaffMessageService {
    @Resource
    private StaffMessageMapper staffMessageMapper;

    /**
     * H04-新增员工
     */
    public Integer add(StaffMessage staffMessage) {
        return staffMessageMapper.add(staffMessage);
    }

    /**
      * 判断是否重复
      * @param username 用户名
      * @return Integer
      */
    public Integer judge(String username) {
        return staffMessageMapper.judge(username);
    }

    /**
     * H05-启用/禁用/修改
     */
    public Integer update(StaffMessage staffMessage) {
        return staffMessageMapper.update(staffMessage);
    }

    /**
     * H01-员工登录
     */
    public List<StaffMessage> longin(StaffMessage staffMessage) {
        List<StaffMessage> a = staffMessageMapper.longin(staffMessage);
        if (a.size() == (0)) {
            return null;
        }
        return a;
    }

    /**
     * H02-员工登出
     */
    @Resource
    private RedisTemplate<String, String> stringRedisTemplate;
    public Boolean staffDelete() {
        return stringRedisTemplate.delete("token");
    }

    /**
     * H03-员工分页查询
     * @param name 名字
     * @param page 页数
     * @param pageSize 每页条数
     */
    public R select(Integer page, Integer pageSize, String name) {
        //构建分页拦截器
        //PageHelper.startPage(m,n)两个参数，第一个参数是页数。第二个参数是条数，每页查询的条数。
        PageHelper.startPage(page, pageSize);
        List<StaffMessage> employeeList = staffMessageMapper.select(name);
        PageInfo<StaffMessage> pageInfo = new PageInfo<>(employeeList);
        //返回值对象，这里的P类是根据前端要的数据专门写的类。属性只有这四条
        Paging p = new Paging();
        //总条数
        p.setTotal(pageInfo.getTotal());
        //每页显示条数
        p.setSize(pageInfo.getSize());
        //当前页码
        p.setCurrent(pageInfo.getPageNum());
        //被展示的数据集
        p.setRecords(employeeList);
        return R.success(p);
    }

    /**
     * H03-根据id查询员工信息
     */
    public StaffMessage findById(Integer id) {
        return staffMessageMapper.findById(id);
    }


}
